package com.winshare.saleAssistant.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winshare.saleAssistant.dao.UserLoginLogMapper;
import com.winshare.saleAssistant.model.UserLoginLog;
import com.winshare.saleAssistant.modelVo.LoginCountVo;
import com.winshare.saleAssistant.service.IUserLoginLogService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 登录日志 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-11-16
 */
@Service
public class UserLoginLogServiceImpl extends ServiceImpl<UserLoginLogMapper, UserLoginLog> implements IUserLoginLogService {


    @Override
    public List<UserLoginLog> selectListByPCA(String province, String city, String areaName) {
        return this.baseMapper.selectListByPCA(province, city, areaName);
    }

    @Override
    public int getTUserLogCount(Date starTime, Date endTime, String productName) {
        return this.baseMapper.getTUserLogCount(starTime, endTime, productName);
    }

    @Override
    public int getLoginLogTotatol(String areaName, String productName, Date starTime, Date endTime) {
        return this.baseMapper.getLoginLogTotatol(areaName, productName, starTime, endTime);
    }

    @Override
    public List<UserLoginLog> getHighTime(String areaName, String productName, Date starTime, Date endTime) {
        return this.baseMapper.getHighTime(areaName, productName, starTime, endTime);
    }

    @Override
    public Map<String, Object> getloginDiscount(String areaName, String productName, Date starTime, Date endTime) {
        return this.baseMapper.getloginDiscount(areaName, productName, starTime, endTime);
    }

    @Override
    public List<LoginCountVo> getMonthAccount(String provinceName, String cityName, String areaName, String productName, Date starTime, Date endTime) {
        List<LoginCountVo> monthAccount = this.baseMapper.getMonthAccount(provinceName, cityName, areaName, productName, starTime, endTime);
        List<LoginCountVo> loginCountVos = new ArrayList<>();
        LoginCountVo loginCountVo1 = monthAccount.get(0);
        LoginCountVo loginCountVo2 = monthAccount.get(0);
        for (LoginCountVo loginCountVo : monthAccount) {
            if (loginCountVo1.getTotal() < loginCountVo.getTotal()) {
                loginCountVo1.setTime(loginCountVo.getTime());
                loginCountVo1.setTotal(loginCountVo.getTotal());
            }
            if (loginCountVo2.getTotal() > loginCountVo.getTotal()) {
                loginCountVo2.setTotal(loginCountVo.getTotal());
                loginCountVo2.setTime(loginCountVo.getTime());
            }
        }
        loginCountVos.add(loginCountVo1);
        loginCountVos.add(loginCountVo2);
        return loginCountVos;
    }

    @Override
    public List<LoginCountVo> getAvgTime(String provinceName, String cityName, String areaName, String productName, Date starTime, Date endTime) {
        int days = (int) ((endTime.getTime() - starTime.getTime()) / (1000 * 3600 * 24));
        List<LoginCountVo> avgTime = this.baseMapper.getAvgTime(provinceName, cityName, areaName, productName, starTime, endTime);
        List<LoginCountVo> loginCountVos = new ArrayList<>();
        LoginCountVo loginCountVo1 = avgTime.get(0);
        LoginCountVo loginCountVo2 = avgTime.get(0);
        for (LoginCountVo loginCountVo : avgTime) {
            if (loginCountVo1.getTotal() < loginCountVo.getTotal()) {
                loginCountVo1.setTime(loginCountVo.getTime());
                loginCountVo1.setTotal(loginCountVo.getTotal() / days);
            }
            if (loginCountVo2.getTotal() > loginCountVo.getTotal()) {
                loginCountVo2.setTotal(loginCountVo.getTotal());
                loginCountVo2.setTime(loginCountVo.getTime() / days);
            }
        }
        loginCountVos.add(loginCountVo1);
        loginCountVos.add(loginCountVo2);
        return loginCountVos;
    }


    @Override
    public List<Map<String, Object>> getPALogin(String productName, Date starTime, Date endTime, String orderBy) {
        return this.baseMapper.getPALogin(productName, starTime, endTime, orderBy);
    }

    @Override
    public List<UserLoginLog> getSchoolCountTime(String provinceName, String cityName, String areaName, String productName, Date starTime, Date endTime, String dataType) {
        return this.baseMapper.getSchoolCountTime(provinceName, cityName, areaName, productName, starTime, endTime, dataType);
    }

    @Override
    public String getSynchronousTime() {
        return this.baseMapper.getSynchronousTime();
    }

}
